package com.defeng.product.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.defeng.common.cache.utils.SystemUserUtils;
import com.defeng.common.core.utils.DateUtils;
import com.defeng.common.core.utils.uuid.IdUtils;
import com.defeng.common.security.utils.SecurityUtils;
import com.defeng.product.domain.MallProductRuleAttrInfo;
import com.defeng.product.domain.module.RuleAttrModule;
import com.defeng.product.service.IMallProductRuleAttrInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.defeng.product.mapper.MallProductRuleInfoMapper;
import com.defeng.product.domain.MallProductRuleInfo;
import com.defeng.product.service.IMallProductRuleInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品规格Service业务层处理
 * 
 * @author XieDeFeng
 * @date 2022-06-25
 */
@Service
public class MallProductRuleInfoServiceImpl implements IMallProductRuleInfoService 
{
    @Autowired
    @SuppressWarnings("all")
    private MallProductRuleInfoMapper mallProductRuleInfoMapper;

    @Autowired
    private IMallProductRuleAttrInfoService mallProductRuleAttrInfoService;

    /**
     * 查询商品规格
     * 
     * @param id 商品规格主键
     * @return 商品规格
     */
    @Override
    public MallProductRuleInfo selectMallProductRuleInfoById(String id)
    {
        return mallProductRuleInfoMapper.selectMallProductRuleInfoById(id);
    }

    /**
     * 查询商品规格列表
     * 
     * @param mallProductRuleInfo 商品规格
     * @return 商品规格
     */
    @Override
    public List<MallProductRuleInfo> selectMallProductRuleInfoList(MallProductRuleInfo mallProductRuleInfo)
    {
        List<MallProductRuleInfo> mallProductRuleInfos = mallProductRuleInfoMapper.selectMallProductRuleInfoList(mallProductRuleInfo);
        mallProductRuleInfos.forEach(productRule ->{
            productRule.setParam("createByName", SystemUserUtils.getUserNameByChche(productRule.getCreateBy()));
        });
        return mallProductRuleInfos;
    }

    /**
     * 新增商品规格
     * 
     * @param mallProductRuleInfo 商品规格
     * @return 结果
     */
    @Override
    @Transactional()
    public int insertMallProductRuleInfo(MallProductRuleInfo mallProductRuleInfo)
    {
        mallProductRuleInfo.setId(IdUtils.simpleUUID());
        mallProductRuleInfo.setCreateBy(SecurityUtils.getUsername());
        mallProductRuleInfo.setCreateTime(DateUtils.getNowDate());

        //批量添加
        mallProductRuleAttrInfoService.batchAdd(ruleAttrToModule(mallProductRuleInfo));
        return mallProductRuleInfoMapper.insertMallProductRuleInfo(mallProductRuleInfo);
    }

    /**
     * 修改商品规格
     * 
     * @param mallProductRuleInfo 商品规格
     * @return 结果
     */
    @Override
    public int updateMallProductRuleInfo(MallProductRuleInfo mallProductRuleInfo)
    {
        mallProductRuleInfo.setUpdateBy(SecurityUtils.getUsername());
        mallProductRuleInfo.setUpdateTime(DateUtils.getNowDate());
        //先去删除
        /**
         *   ArrayList<String> idList = new ArrayList<>();
         *   String[] ids = idList.toArray(new String[idList.size()]);
         *   new String[]{mallProductRuleInfo.getId()}*/
        mallProductRuleAttrInfoService.deleteByRuleIds(new String[]{mallProductRuleInfo.getId()});
        //再去批量添加
        mallProductRuleAttrInfoService.batchAdd(ruleAttrToModule(mallProductRuleInfo));
        return mallProductRuleInfoMapper.updateMallProductRuleInfo(mallProductRuleInfo);
    }

    /**
     * 把前端传入的ruleAttr属性json字符串转为RuleAttrModule业务对象
     * @param mallProductRuleInfo
     * @return
     */
    private List<MallProductRuleAttrInfo> ruleAttrToModule(MallProductRuleInfo mallProductRuleInfo){
        //去对规格的属性进行存储
        String ruleAttr = mallProductRuleInfo.getRuleAttr();
        List<RuleAttrModule> ruleAttrModules = JSONObject.parseArray(ruleAttr, RuleAttrModule.class);
        ArrayList<MallProductRuleAttrInfo> mallProductRuleAttrInfos = new ArrayList<>();
        ruleAttrModules.forEach(ruleAttrModule -> {
            String ruleAttrModuleKey = ruleAttrModule.getKey();
            ruleAttrModule.getValues().forEach(value ->{
                MallProductRuleAttrInfo mallProductRuleAttrInfo = new MallProductRuleAttrInfo();
                mallProductRuleAttrInfo.setId(IdUtils.simpleUUID());
                mallProductRuleAttrInfo.setRuleId(mallProductRuleInfo.getId());
                mallProductRuleAttrInfo.setName(ruleAttrModuleKey);
                mallProductRuleAttrInfo.setAttrValue(value);
                mallProductRuleAttrInfo.setCreateBy(SecurityUtils.getUsername());
                mallProductRuleAttrInfo.setCreateTime(new Date());
                mallProductRuleAttrInfos.add(mallProductRuleAttrInfo);
            });
        });
        return mallProductRuleAttrInfos;
    }

    /**
     * 批量删除商品规格
     * 
     * @param ids 需要删除的商品规格主键
     * @return 结果
     */
    @Override
    public int deleteMallProductRuleInfoByIds(String[] ids)
    {
        mallProductRuleAttrInfoService.deleteByRuleIds(ids);

        return mallProductRuleInfoMapper.deleteMallProductRuleInfoByIds(ids);
    }

    /**
     * 删除商品规格信息
     * 
     * @param id 商品规格主键
     * @return 结果
     */
    @Override
    public int deleteMallProductRuleInfoById(String id)
    {


        return mallProductRuleInfoMapper.deleteMallProductRuleInfoById(id);
    }

}
